package com.java.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.java.model.UserSession;

public final class HttpSessionUtils
{

    private static String getMethodName( String key )
    {
        if ( key == null || key.length() < 2 )
        {
            return key;
        }
        return "get" + key.substring( 0, 1 ).toUpperCase() + key.substring( 1 );
    }

    /**
     * @param request
     * @return 로그인 아이디 
     */
    public static String getLoginId( HttpServletRequest request )
    {
        Object obj = request.getSession().getAttribute( "user" );
        if ( obj == null )
        {
            return null;
        }
        UserSession user = (UserSession) obj;
        return user.getLogin_id();
    }

    /**
     * @param request
     * @return 로그인 사용자명 
     */
    public static String getUserName( HttpServletRequest request )
    {
        Object obj = request.getSession().getAttribute( "user" );
        if ( obj == null )
        {
            return null;
        }
        UserSession user = (UserSession) obj;
        return user.getUser_name();
    }

    /**
     * @param request
     * @param key UserSession 의 프로퍼티명 
     * @return key 에 해당하는 value
     * @throws InvocationTargetException 
     * @throws IllegalAccessException 
     * @throws IllegalArgumentException 
     */
    public static String getValueByKey( HttpServletRequest request, String key )
    {
        Object obj = request.getSession().getAttribute( "user" );
        if ( obj == null )
        {
            return null;
        }
        UserSession user = (UserSession) obj;
        String methodName = getMethodName( key );
        Class[] clazz = null;
        try
        {
            Method method = user.getClass().getDeclaredMethod( methodName, clazz );
            return (String) method.invoke( user, null );
        }
        catch ( SecurityException e )
        {
            return null;
        }
        catch ( NoSuchMethodException e )
        {
            return null;
        }
        catch ( IllegalArgumentException e )
        {
            return null;
        }
        catch ( IllegalAccessException e )
        {
            return null;
        }
        catch ( InvocationTargetException e )
        {
            return null;
        }
    }

    /**
     * @return userSession UserSession 사용자 세션정보를 리턴
     * @param request 
     */
    public static UserSession getUserSession( HttpServletRequest request )
    {
        Object obj = request.getSession().getAttribute( "user" );
        if ( obj != null )
        {
            return (UserSession) obj;
        }
        return null;
    }

    /**
     * 사용자 세션정보를 설정 
     * @param request
     * @param userSession
     */
    public static void setUserSession( HttpServletRequest request, UserSession userSession )
    {
        request.getSession().setAttribute( "user", userSession );
    }

    /**
     * HttpMap Parameter 에 세션정보를 설정 한다.
     * @param request HttpServletRequest
     * @param requestMap HttpMap 파라미터
     */
    public static void setSessionRequestMap( HttpServletRequest request, HttpMap requestMap )
    {

        Object obj = request.getSession().getAttribute( "user" );
        if ( obj == null || requestMap == null )
        {
            return;
        }
        UserSession user = (UserSession) obj;
        if ( !requestMap.isSetKeys( "user_id" ) )
        {
            requestMap.setString( "user_id", user.getUser_id() );
        }
        if ( !requestMap.isSetKeys( "user_name" ) )
        {
            requestMap.setString( "user_name", user.getUser_name() );
        }
        if ( !requestMap.isSetKeys( "group_id" ) )
        {
            requestMap.setString( "group_id", user.getGroup_id() );
        }
        if ( !requestMap.isSetKeys( "login_id" ) )
        {
            requestMap.setString( "login_id", user.getLogin_id() );
        }
        if ( !requestMap.isSetKeys( "login_fail_cnt" ) )
        {
            requestMap.setString( "login_fail_cnt", user.getLogin_fail_cnt() );
        }
        if ( !requestMap.isSetKeys( "active" ) )
        {
            requestMap.setString( "active", user.getZipcode() );
        }
        if ( !requestMap.isSetKeys( "RealPath" ) )
        {
            requestMap.setString( "RealPath", (String) request.getRealPath( "/" ) );
        }
    }

    public static void setSessionRequestMap( HttpServletRequest request, List parameterList )
    {

        Object obj = request.getSession().getAttribute( "user" );
        if ( obj == null || parameterList == null )
        {
            return;
        }

        for ( int i = 0; i < parameterList.size(); i++ )
        {
            HttpMap map = (HttpMap) parameterList.get( i );
            setSessionRequestMap( request, map );
        }

    }
}
